import {Dimensions,Platform} from 'react-native';
// import Globle from './Globle';
const deviceWidth = Dimensions.get('window').width;
const deviceHeight = Dimensions.get('window').height;
const defaultPixel = 2;
const w2 = 375 / defaultPixel;
const h2 = 667 / defaultPixel;
const scale = Math.min(deviceHeight/h2,deviceWidth/w2);
const defaultImg = {
  "dapp":require("../images/dapp.png")
}

const width = () =>{
  return deviceWidth;
}

const height = () => {
  return deviceHeight;
}

const size = (size) =>{
  size = Math.round(size * scale + 0.5);
  return size / defaultPixel;
}

isIphoneX = () =>{
  if(Platform.OS==="ios"){
    if(deviceHeight==812){
      return true;
    }
  }
  return false;
}

isIos = () =>{
  if(Platform.OS==="ios"){
    return true;
  }
  return false;
}

isEmpty = (tx) =>{
  if(!tx || tx.length==0){
    return true;
  }
  return false;
}

head = (img) =>{
  if(!img)img="https://hxblock.oss-cn-shenzhen.aliyuncs.com/default_header.png";
  if(!img.startsWith("http") && !img.startsWith("https"))return {uri:img};
  if(img.indexOf("_300")!=-1||img.indexOf("_150")!=-1||img.indexOf("_600")!=-1){
    return {uri:img};
  }
  return {uri:img+"_150"};
}

image = (img) =>{
  if(img && (img.startsWith("http") || img.startsWith("https"))){
    return {uri:img};
  }else{
    return defaultImg[img];
  }
}

const imImage = (url) =>{
  if(Platform.OS=="ios"){
    return {uri:url};
  }else{
    return {uri:url};
  }

}


const getAsetss = (asetss) =>{
  try{
    let s = asetss.split(" ");
    if(s.length>0){
      let price = s[0];
      return parseFloat(price);
    }
  }catch(e){
    return 0
  }
}

const numFormat = (num) =>{
  if(num===0){return "0"}
  if(!num){return ""}
  num.toString().indexOf(".")!=-1 && (num  = num.toFixed(4));
  let n = num.toString().split(".");
  num = n[0];
  return (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')+((n.length>1)?'.'+n[1]:'');
}


const numFormatScale = (num,scale) =>{
  if(num===0){return "0"}
  if(!num){return ""}
  num.toString().indexOf(".")!=-1 && (num  = num.toFixed(scale));
  let n = num.toString().split(".");
  num = n[0];
  return (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')+((n.length>1)?'.'+n[1]:'');
}

const moneyFormat = (num) =>{
  if(num>=100000000){
      return (num/100000000).toFixed(2)+"亿";
  }else if(num>10000){
      return (num/10000).toFixed(2)+"万";
  }else{
      return num;
  }
}

const ramFormater = (limit) => {
  let size = "";
  if(limit < 0.1 * 1024){
      size = limit.toFixed(2) + " B"
  }else if(limit < 0.1 * 1024 * 1024){
      size = (limit/1024).toFixed(2) + " KB"
  }else if(limit < 0.1 * 1024 * 1024 * 1024){
      size = (limit/(1024 * 1024)).toFixed(2) + " MB"
  }else{
      size = (limit/(1024 * 1024 * 1024)).toFixed(2) + " GB"
  }
  let sizeStr = size + "";
  let index = sizeStr.indexOf(".");
  let dou = sizeStr.substr(index + 1 ,2);
  if(dou == "00"){
      return sizeStr.substring(0, index) + sizeStr.substr(index + 3, 2)
  }
  return size;
}

cpuFormater = (limit) =>{
  let size = "";
  if(limit < 60 * 1000000){
      size = limit/1000 + "ms"
  }else{
      size = (limit/(60*60*1000000)).toFixed(2) + "hr"
  }
  let sizeStr = size + "";
  let index = sizeStr.indexOf(".");
  let dou = sizeStr.substr(index + 1 ,2);
  if(dou == "00"){
      return sizeStr.substring(0, index) + sizeStr.substr(index + 3, 2)
  }
  return size;
}

const refunTime = (time) =>{
  let dt = new Date(time).getTime();
  let now = new Date().getTime();
  let fx = (now-dt)/1000/60;
  let max = 3*24*60;
  if(fx>max){
    return null;
  }else{
    return Math.ceil((max-fx)/60)+"小时"
  }
}


const friendCovert = (src) =>{
    let letters = "*ABCDEFGHIJKLMNOPQRSTUVWXYZ".split('');
    let segs = [];
    let curr;
    let index = 0;
    letters.map((lt)=>{
        curr = {title:lt,data:[],index};
        src.map((item)=>{
            let name = item.nickname;
            if(item.notename && item.notename.length>0){
                name = item.notename;
            }
            let p = pinyin.getFirstLetter(name);
            if(p==""){
                p="*";
            }else{
                p = p.toUpperCase();
            }
            if(p==lt){
                curr.data.push(item);
            }
        });
        if(curr.data.length>0){
            segs.push(curr);
            index = index+1;
            index = index+curr.data.length;
        }
    });
    return segs;
}

export default {
  isEmpty,
  size,
  width,
  height,
  isIphoneX,
  isIos,
  head,
  image,
  getAsetss,
  numFormat,
  moneyFormat,
  ramFormater,
  cpuFormater,
  refunTime,
  imImage,
  numFormatScale
};
